# Zsh-Example
# This example file is borrowed from zsh sources in Functions/Misc/xtermctl
# Last modification: 2020-05-02 16:20:17


typeset -Ag xtermseq
xtermseq=(
  1      deiconify             2    iconify
 '3;X;Y' position
 '4;H;W' size_pixels
  5      raise                 6    lower
  7      refresh
 '8;H;W' size_chars
 '9;0'   unmaximize           '9;1' maximize
 11      get_iconify
 13      get_position
 14      get_size_pixels
 18      get_size_chars
 19      get_screen_chars
 20      get_label
 21      get_title
)

local k
for k in ${(k)xtermseq}; do 
	xtermseq[${xtermseq[$k]}]=$k
done

function xterm-tell {
  local seq=${${${xtermseq[$1]:?no such control}/[HX]/$2}/[WY]/$3}
  print -nr -- $'\e['"${seq}"t
}

# The following use of "read -st 2 ..." with the control sequences in
# the prompt string requires zsh 4.3.5-dev-1 or later (zsh-users/12600
# or equivalent patch).

function xterm-ask {
  local esc
  unset REPLY reply
  1=get_${1#get_}
  local seq=${xtermseq[$1]:?no such control}
  case $1 in
  (get_(label|title))
      read -st 2 -rk 3 esc$'?\e['"${seq}"t || return 1
      read -srd $'\e'
      read -srk 1 esc
      ;;
  (get_*)
      read -st 2 -rk 2 esc$'?\e['"${seq}"t || return 1
      IFS=';' read -Arsd t
      (( $#reply > 2 )) && shift reply
      ;;
  esac
  return 0
}

local documentation; read -rd $'\e' documentation <<'EOdoc' <<<$'\e'

CSI = "control sequence introducer": ESC [
OSC = "operating system command": ESC ]
ST = "string terminator": ESC backslash
Ps = "parameter string": (see list below)

All control sequences described here begin with CSI and end with "t".
Note that there are no spaces in control sequences or responses,
except possibly for the text responses for label and title; spaces
shown below are for readability.

[snip]

EOdoc

[[ -n "${(M)@:#--explain}" ]] && print "$documentation"

return 0
